package com.iflytek.ci.qams.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.iflytek.ci.qams.dao.ActionBatchDao;
import com.iflytek.ci.qams.dao.ActionDao;
import com.iflytek.ci.qams.dao.CommentDao;
import com.iflytek.ci.qams.dao.EasyCommentDao;
import com.iflytek.ci.qams.domain.Action;
import com.iflytek.ci.qams.domain.Comment;

@Repository("ActionBatchDao")
public class ActionBatchDaoImpl implements ActionBatchDao {

	@Autowired
	private EasyCommentDao easycommentDao;
	/** 注入批处理的SqlSessionTemplate */
	@Qualifier("batchSqlSession")
	@Autowired
	private SqlSessionTemplate batchSqlSession;

	@Override
	public void batch_action(List<Action> actions) {
		SqlSession sqlSession = batchSqlSession.getSqlSessionFactory()
				.openSession(ExecutorType.BATCH);
		List<Comment>comments=new ArrayList<Comment>();
		ActionDao actionDao = sqlSession.getMapper(ActionDao.class);
		for (Action action : actions) {
            actionDao.add_issue_action(action);
            Comment comment=new Comment();
            comment.setAction_id(actionDao.last_id());
            comment.setIssue(action.getIssue());
            comments.add(comment);
		}
		sqlSession.flushStatements();
		sqlSession.commit();
		sqlSession.close();
		easycommentDao.add_comments(comments);
	}
}
